package org.jeecg.modules.demo.crm.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.Md5Util;
import org.jeecg.modules.demo.crm.entity.CEmail;
import org.jeecg.modules.demo.crm.entity.CQuotationInfo;
import org.jeecg.modules.demo.crm.entity.CQuotationMail;
import org.jeecg.modules.demo.crm.entity.CQuotationMaterials;
import org.jeecg.modules.demo.crm.mapper.CEmailMapper;
import org.jeecg.modules.demo.crm.mapper.CQuotationInfoMapper;
import org.jeecg.modules.demo.crm.mapper.CQuotationMailMapper;
import org.jeecg.modules.demo.crm.mapper.CQuotationMaterialsMapper;
import org.jeecg.modules.demo.crm.service.ICEmailService;
import org.jeecg.modules.demo.crm.service.ICQuotationMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 工程报价邮件发送
 * @Author: jeecg-boot
 * @Date:   2025-09-01
 * @Version: V1.0
 */
@Service
public class CQuotationMailServiceImpl extends ServiceImpl<CQuotationMailMapper, CQuotationMail> implements ICQuotationMailService {

    @Autowired
    private CQuotationMailMapper cQuotationMailMapper;

    @Autowired
    private CQuotationInfoMapper cQuotationInfoMapper;

    @Autowired
    private CEmailMapper cemailMapper;


    @Autowired
    private ICEmailService emailService;

    @Autowired
    private CQuotationMaterialsMapper cQuotationMaterialsMapper;

    @Override
    public void add(CQuotationMail cQuotationMail) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        cQuotationMail.setUserId(sysUser.getId());
        cQuotationMailMapper.insert(cQuotationMail);
        //记录邮件发送
        String status ="待发送";
        String msg = "";
        String attachmentUrl = "";
        String[] urls = cQuotationMail.getAttachmentUrl().split(",");
        for (String url : urls) {
            if (url.trim().endsWith(".pdf")) {
                attachmentUrl= url.trim();
                break;
            }
        }
        attachmentUrl = attachmentUrl.replace("http://154.201.66.67:9000","http://gm.aattrader.com/minio");
        String content = cQuotationMail.getContent().replace("http://154.201.66.67:9000","http://gm.aattrader.com/minio");


        String dataKey = Md5Util.md5Encode(cQuotationMail.getSubject() + "-" + cQuotationMail.getUserId() +'_'+cQuotationMail.getContent()+ " - " + cQuotationMail.getUserId(),"utf-8");
        CEmail cemail = CEmail.builder()
                .userEmail(cQuotationMail.getReceivingMaill())
                .attachedMaill(cQuotationMail.getAttachedMaill())
                .subject(cQuotationMail.getSubject())
                .userId(cQuotationMail.getUserId())
                .content(content)
                .attachmentUrl(attachmentUrl)
                .status(status)
                .msg("")
                .code("")
                .dataKey(dataKey)
                .build();
        try {
            status ="发送成功！！";
            msg = "发送成功！！";
            cemail.setStatus(status);
            cemail.setMsg(msg);
            emailService.sendNeteaseEmail(cemail);

        } catch (Exception e) {
            msg = e.getMessage();
            status ="发送失败！！";
            cemail.setStatus(status);
            cemail.setMsg(msg);
        }
        cemailMapper.insert(cemail);
        cQuotationMail.setStatus(status);
        cQuotationMail.setMsg(msg);
        cQuotationMailMapper.updateById(cQuotationMail);
        //把记录存入数据库
        List<CQuotationInfo> details = cQuotationInfoMapper.details(cQuotationMail.getQuotationId());
        for (CQuotationInfo detail : details) {
            if(detail != null && StringUtils.isNotEmpty(detail.getName())){
                //System.out.println(detail.getName());
              /*  CQuotationMaterials materials = cQuotationMaterialsMapper.findByName(detail.getName());
                if(materials == null){
                    materials = new CQuotationMaterials();
                }*/
                CQuotationMaterials materials = new CQuotationMaterials();
                materials.setMainMaterial(detail.getMainMaterial());
                materials.setAuxMaterial(detail.getAuxMaterial());
                materials.setQuotationId(cQuotationMail.getQuotationId());
                materials.setUnit(detail.getUnit());
                materials.setQuantity(detail.getQuantity());
                materials.setLabor(detail.getLabor());
                if (materials.getId() ==null){
                    cQuotationMaterialsMapper.insert(materials);
                }else {
                    cQuotationMaterialsMapper.updateById(materials);
                }
            }

        }
    }
}
